package com.example.uit_management;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONObject;

import android.util.Log;
/** Theo tác trên sever RegCourse(Đăng kí sinh viên)	 */
public class JsonParser_regCourse {	
	// Biến Mã sinh viên cần lấy
		private String Id_Student=MainActivity.Student_ID;
		//////////////////////////////////////////
		// Cac Bien trong Json tra ve
		////////////////////////////////////////
		/** Key Điểm số môn học 1	 */
		static final String KEY_score1 = "score1"; 
		/** Key Điểm số môn học 2	 */
		static final String KEY_score2 = "score2"; // 
		/** Key Điểm số môn học 3	 */
		static final String KEY_score3 = "score3"; 
		/** Key Điểm tổng kết	 */
		static final String KEY_scoreFinal = "scoreFinal"; // 
		/** Key Mã lớp	 */
		static final String KEY_courseID = "courseID";		// 
		/** Key Mã số sinh viên	 */
		static final String KEY_studentID = "studentID";	// 
		/** Key Mã đăng kí	 */
		static final String KEY_regCourseID = "regCourseID";	// 
		/** Key Tên sinh viên	 */
		static final String KEY_studentName = "studentName";	// 
		/** Key nam hoc	 */
		static final String KEY_semesterYear = "semesterYear";	// 
		/** Key nam hoc	ky */
		static final String KEY_semesterIndex = "semesterIndex";	// 
		/** Key ma mon hoc */
		static final String  KEY_subjectID = "subjectID";
		/** Key tong tinh chi */
		static final String  KEY_subjectCredit = "subjectCredit";	
		/** Key ten mon */
		static final String  KEY_subjectName = "subjectName";
		/** Key thu */
		static final String  KEY_courseDay = "courseDay";
		/** Key ca*/
		static final String  KEY_courseTime = "courseTime";
		/** Key phong */
		static final String  KEY_roomID = "roomID";	
		/** loai mon hoc */
		static final String  KEY_regCourseType = "regCourseType";	
		/** ten giao vien */
		static final String KEY_lecturerName ="lecturerName";
		
		//////////////////////////////////////////////////////////
		//Các biến lưu chuổi lấy về cho các Từ khóa trên
		//////////////////////////////////////////////////////////
		/** Biến cục bộ Điểm số môn học 1	 */
		static  String score1 = "score1"; 
		/** Biến cục bộ Điểm số môn học 2	 */
		static  String score2 = "score2";
		/** Biến cục bộ Điểm số môn học 3	 */
		static  String score3 = "score3";
		/** Biến cục bộ Điểm tổng kết	 */
		static  String scoreFinal = "scoreFinal";
		/** Biến cục bộ Mã lớp	 */
		static  String courseID = "courseID";
		/** Biến cục bộ Mã số sinh viên	 */
		static  String studentID = "studentID";
		/** Biến cục bộ Mã Đăng kí	 */
		static  String regCourseID = "regCourseID";
		/** Biến cục bộ Tên sinh viên	 */
		static  String studentName = "studentName";
		/** Biến cục bộ nam hoc	 */
		static  String semesterYear = "semesterYear";
		/** Biến cục bộ hoc	ky */
		static  String semesterIndex = "semesterIndex";
		/** Biến cục bộ ma mon hoc */
		static  String subjectID = "subjectID";
		/** Biến cục bộ tong tinh chi */
		static String  subjectCredit = "subjectCredit";
		/** Biến cục bộ ten mon */
		static String  subjectName = "subjectName";	
		
		/** Biến cục bộ thu */
		static String  courseDay = "courseDay";
		/** Biến cục bộ ca*/
		static String  courseTime = "courseTime";
		/** Biến cục bộ phong */
		static String  roomID = "roomID";	
		/** Choi lien ket	 */
		/** loai mon hoc */
		static String  regCourseType = "regCourseType";
		/** ten giao vien */
		static String  lecturerName = "lecturerName";
		//link http://uitstudentmanagement.appspot.com/regCourse?type=bystudent&studentid=08520230
	    // va
		// link 5.	http://uitstudentmanagement.appspot.com/regCourse?type=score&studentid=08520211
		// ke ca mon rot
		

	/** Hàm khởi tạo một JsonParse_regCourse */
	public JsonParser_regCourse() {
		// TODO Auto-generated constructor stub
	}
	/** Dữ liệu trả về là Array Json ta cần(Lấy theo mã sinh viên)*/
	public JSONArray GetJSONArray_regCourse(){		
		String readTwitterFeed = readTwitterFeed();					
			try
	        {
				// Lấy Json từ sever cung cấp(là Data) Sau đó lấy mảng Array từ Data đó
				JSONObject json = new JSONObject(readTwitterFeed);
				JSONArray jsonArray = json.getJSONArray("data");	
				return jsonArray;
	        }
	        catch (Exception je)
	        {	   
	            Log.e(JsonParser_regCourse.class.toString(), "Method GetJSONArray_regCourse of JsonParser_regCourse");
	        	return null;
	        }	  				
	}
	public JSONArray GetJSONArray_regCourse_score(){		
		String readTwitterFeed = readTwitterFeed_score();					
			try
	        {
				// Lấy Json từ sever cung cấp(là Data) Sau đó lấy mảng Array từ Data đó
				JSONObject json = new JSONObject(readTwitterFeed);
				JSONArray jsonArray = json.getJSONArray("data");	
				return jsonArray;
	        }
	        catch (Exception je)
	        {	   
	            Log.e(JsonParser_regCourse.class.toString(), "Method GetJSONArray_regCourse of JsonParser_regCourse");
	        	return null;
	        }	  				
	}
	/** Hàm lấy về Json do Sever cung cấp(o đây là Json là Data) theo mã số sinh viên*/
	public String readTwitterFeed() {
		// Các biến cần thiết cho việc lấy JSon từ sever về
	    StringBuilder builder = new StringBuilder();
	    HttpClient client = new DefaultHttpClient();
	    //HttpGet httpGet = new HttpGet("http://uitstudentmanagement.appspot.com/regCourse");
	    HttpGet httpGet =new HttpGet("http://uitstudentmanagement.appspot.com/regCourse?type=bystudent&studentid="+MainActivity.Student_ID);
	    
	    // Lấy dữ liệu về
	    try {
	        HttpResponse response = client.execute(httpGet);
	        StatusLine statusLine = response.getStatusLine();
	        int statusCode = statusLine.getStatusCode();
	        if (statusCode == 200) {
	            HttpEntity entity = response.getEntity();
	            InputStream content = entity.getContent();
	            BufferedReader reader = new BufferedReader(new InputStreamReader(content));
	            String line;
	            while ((line = reader.readLine()) != null) {
	                builder.append(line);
	            }
	        } else {
	            Log.e(JsonParser_regCourse.class.toString(), "Method readTwtterFeed of JsonParser_regCourse statusCode != 200");
	        }
	    } catch (ClientProtocolException e) {
	        e.printStackTrace();
	    } catch (IOException e) {
	        e.printStackTrace();
	    }
	    // Dữ liệu được trả về
	    return builder.toString();
	}
	/** Hàm lấy về Json do Sever cung cấp(o đây là Json là Data) theo mã số sinh viên*/
	public String readTwitterFeed_score() {
		// Các biến cần thiết cho việc lấy JSon từ sever về
	    StringBuilder builder = new StringBuilder();
	    HttpClient client = new DefaultHttpClient();
	    //HttpGet httpGet = new HttpGet("http://uitstudentmanagement.appspot.com/regCourse");
	    HttpGet httpGet =new HttpGet("http://uitstudentmanagement.appspot.com/regCourse?type=coursemainbystudent&studentid="+MainActivity.Student_ID);
	    
	    // Lấy dữ liệu về
	    try {
	        HttpResponse response = client.execute(httpGet);
	        StatusLine statusLine = response.getStatusLine();
	        int statusCode = statusLine.getStatusCode();
	        if (statusCode == 200) {
	            HttpEntity entity = response.getEntity();
	            InputStream content = entity.getContent();
	            BufferedReader reader = new BufferedReader(new InputStreamReader(content));
	            String line;
	            while ((line = reader.readLine()) != null) {
	                builder.append(line);
	            }
	        } else {
	            Log.e(JsonParser_regCourse.class.toString(), "Method readTwtterFeed of JsonParser_regCourse statusCode != 200");
	        }
	    } catch (ClientProtocolException e) {
	        e.printStackTrace();
	    } catch (IOException e) {
	        e.printStackTrace();
	    }
	    // Dữ liệu được trả về
	    return builder.toString();
	}	
	/**
	 *Hàm trả về Array list HashMap từ sever regCourse(Các môn SV đã đăng kí)
	 */
	public ArrayList<HashMap<String, String>> getMap()
	{
		// Biến Array list hashMap
		ArrayList<HashMap<String, String>> ArrayMap = new ArrayList<HashMap<String, String>>();
		JSONArray jsonArray = GetJSONArray_regCourse();
		// looping through all jsonarray nodes 
		Log.e(JsonParser_regCourse.class.toString(), "Tatol member :"+jsonArray.length());
		for (int i = 0; i < jsonArray.length(); i++) {
			try {
				//create new JsonObject	
				JSONObject js = jsonArray.getJSONObject(i);
				// creating new HashMap
				HashMap<String, String> map = new HashMap<String, String>();			
				// adding each child node to HashMap key => value			
				score1		=js.getString("score1");
				Log.e(JsonParser_regCourse.class.toString(), "score1 :"+score1+"_____Get String score1 JsonParser_regCourse");
				score2 		=js.getString("score2");
				Log.e(JsonParser_regCourse.class.toString(), "score2:"+score2+"_____Get String score2 JsonParser_regCourse");
				score3	=js.getString("score3");
				Log.e(JsonParser_regCourse.class.toString(), "score3:"+score3+"_____Get String score3 JsonParser_regCourse");
				scoreFinal		=js.getString("scoreFinal");	
				Log.e(JsonParser_regCourse.class.toString(), "scoreFinal:"+scoreFinal+"_____Get String scoreFinal JsonParser_regCourse");
				courseID		=js.getString("courseID");
				Log.e(JsonParser_regCourse.class.toString(), "courseID:"+courseID+"_____Get String courseID JsonParser_regCourse");
				studentID	=js.getString("studentID");
				Log.e(JsonParser_regCourse.class.toString(), "studentID:"+studentID+"_____Get String studentID JsonParser_regCourse");
				regCourseID	=js.getString("regCourseID");
				Log.e(JsonParser_regCourse.class.toString(), "regCourseID:"+regCourseID+"_____Get String regCourseID JsonParser_regCourse");
				studentName	=js.getString("studentName");
				Log.e(JsonParser_regCourse.class.toString(), "studentName:"+studentName+"_____Get String studentName JsonParser_regCourse");
				semesterYear=js.getString("semesterYear");
				Log.e(JsonParser_regCourse.class.toString(), "semesterYear:"+semesterYear+"_____Get String studentName JsonParser_regCourse");
				semesterIndex=js.getString("semesterIndex");
				Log.e(JsonParser_regCourse.class.toString(), "semesterIndex:"+semesterIndex+"_____Get String studentName JsonParser_regCourse");
				subjectID=js.getString("subjectID");
				Log.e(JsonParser_regCourse.class.toString(), "subjectID:"+subjectID+"_____Get String studentName JsonParser_regCourse");
				subjectCredit=js.getString("subjectCredit");
				Log.e(JsonParser_regCourse.class.toString(), "subjectCredit:"+subjectCredit+"_____Get String studentName JsonParser_regCourse");
				subjectName=js.getString("subjectName");
				Log.e(JsonParser_regCourse.class.toString(), "subjectName:"+subjectName+"_____Get String studentName JsonParser_regCourse");
				courseDay=js.getString("courseDay");
				Log.e(JsonParser_regCourse.class.toString(), "courseDay:"+courseDay+"_____Get String studentName JsonParser_regCourse");
				courseTime=js.getString("courseTime");
				Log.e(JsonParser_regCourse.class.toString(), "courseTime:"+courseTime+"_____Get String studentName JsonParser_regCourse");
				roomID=js.getString("roomID");
				Log.e(JsonParser_regCourse.class.toString(), "roomID:"+roomID+"_____Get String studentName JsonParser_regCourse");
				regCourseType=js.getString("regCourseType");
				Log.e(JsonParser_regCourse.class.toString(), "regCourseType:"+regCourseType+"_____Get String studentName JsonParser_regCourse");
				lecturerName=js.getString("lecturerName");
				Log.e(JsonParser_regCourse.class.toString(), "lecturerName:"+lecturerName+"_____Get String studentName JsonParser_regCourse");
				map.put(KEY_score1, score1);	
				map.put(KEY_score2, score2);	
				map.put(KEY_score3, score3);	
				map.put(KEY_scoreFinal, scoreFinal);	
				map.put(KEY_courseID, courseID);	
				map.put(KEY_studentID, studentID);	
				map.put(KEY_regCourseID, regCourseID);	
				map.put(KEY_studentName, studentName);		
				map.put(KEY_semesterYear, semesterYear);	
				map.put(KEY_semesterIndex, semesterIndex);	
				map.put(KEY_subjectID, subjectID);	
				map.put(KEY_subjectCredit, subjectCredit);
				map.put(KEY_subjectName, subjectName);
				map.put(KEY_courseDay, courseDay);
				map.put(KEY_courseTime, courseTime);
				map.put(KEY_roomID, roomID);
				map.put(KEY_regCourseType, regCourseType);
				map.put(KEY_lecturerName, lecturerName);
				
				ArrayMap.add(map);					
			} catch (Exception e) {
				e.printStackTrace();
				// TODO: handle exception
				//Log.e(JsonParser_regCourse.class.toString(), "Exception Method GetMapKey");
			}
			
		}		
		return ArrayMap;
	}	
	public ArrayList<HashMap<String, String>> getMap_score()
	{
		// Biến Array list hashMap
		ArrayList<HashMap<String, String>> ArrayMap = new ArrayList<HashMap<String, String>>();
		JSONArray jsonArray = GetJSONArray_regCourse_score();
		// looping through all jsonarray nodes 
		Log.e(JsonParser_regCourse.class.toString(), "Tatol member :"+jsonArray.length());
		for (int i = 0; i < jsonArray.length(); i++) {
			try {
				//create new JsonObject	
				JSONObject js = jsonArray.getJSONObject(i);
				// creating new HashMap
				HashMap<String, String> map = new HashMap<String, String>();			
				// adding each child node to HashMap key => value			
				score1		=js.getString("score1");
				Log.e(JsonParser_regCourse.class.toString(), "score1 :"+score1+"_____Get String score1 JsonParser_regCourse");
				score2 		=js.getString("score2");
				Log.e(JsonParser_regCourse.class.toString(), "score2:"+score2+"_____Get String score2 JsonParser_regCourse");
				score3	=js.getString("score3");
				Log.e(JsonParser_regCourse.class.toString(), "score3:"+score3+"_____Get String score3 JsonParser_regCourse");
				scoreFinal		=js.getString("scoreFinal");	
				Log.e(JsonParser_regCourse.class.toString(), "scoreFinal:"+scoreFinal+"_____Get String scoreFinal JsonParser_regCourse");
				courseID		=js.getString("courseID");
				Log.e(JsonParser_regCourse.class.toString(), "courseID:"+courseID+"_____Get String courseID JsonParser_regCourse");
				studentID	=js.getString("studentID");
				Log.e(JsonParser_regCourse.class.toString(), "studentID:"+studentID+"_____Get String studentID JsonParser_regCourse");
				regCourseID	=js.getString("regCourseID");
				Log.e(JsonParser_regCourse.class.toString(), "regCourseID:"+regCourseID+"_____Get String regCourseID JsonParser_regCourse");
				studentName	=js.getString("studentName");
				Log.e(JsonParser_regCourse.class.toString(), "studentName:"+studentName+"_____Get String studentName JsonParser_regCourse");
				semesterYear=js.getString("semesterYear");
				Log.e(JsonParser_regCourse.class.toString(), "semesterYear:"+semesterYear+"_____Get String studentName JsonParser_regCourse");
				semesterIndex=js.getString("semesterIndex");
				Log.e(JsonParser_regCourse.class.toString(), "semesterIndex:"+semesterIndex+"_____Get String studentName JsonParser_regCourse");
				subjectID=js.getString("subjectID");
				Log.e(JsonParser_regCourse.class.toString(), "subjectID:"+subjectID+"_____Get String studentName JsonParser_regCourse");
				subjectCredit=js.getString("subjectCredit");
				Log.e(JsonParser_regCourse.class.toString(), "subjectCredit:"+subjectCredit+"_____Get String studentName JsonParser_regCourse");
				subjectName=js.getString("subjectName");
				Log.e(JsonParser_regCourse.class.toString(), "subjectName:"+subjectName+"_____Get String studentName JsonParser_regCourse");
				courseDay=js.getString("courseDay");
				Log.e(JsonParser_regCourse.class.toString(), "courseDay:"+courseDay+"_____Get String studentName JsonParser_regCourse");
				courseTime=js.getString("courseTime");
				Log.e(JsonParser_regCourse.class.toString(), "courseTime:"+courseTime+"_____Get String studentName JsonParser_regCourse");
				roomID=js.getString("roomID");
				Log.e(JsonParser_regCourse.class.toString(), "roomID:"+roomID+"_____Get String studentName JsonParser_regCourse");
				regCourseType=js.getString("regCourseType");
				Log.e(JsonParser_regCourse.class.toString(), "regCourseType:"+regCourseType+"_____Get String studentName JsonParser_regCourse");
				lecturerName=js.getString("lecturerName");
				Log.e(JsonParser_regCourse.class.toString(), "lecturerName:"+lecturerName+"_____Get String studentName JsonParser_regCourse");
				map.put(KEY_score1, score1);	
				map.put(KEY_score2, score2);	
				map.put(KEY_score3, score3);	
				map.put(KEY_scoreFinal, scoreFinal);	
				map.put(KEY_courseID, courseID);	
				map.put(KEY_studentID, studentID);	
				map.put(KEY_regCourseID, regCourseID);	
				map.put(KEY_studentName, studentName);		
				map.put(KEY_semesterYear, semesterYear);	
				map.put(KEY_semesterIndex, semesterIndex);	
				map.put(KEY_subjectID, subjectID);	
				map.put(KEY_subjectCredit, subjectCredit);
				map.put(KEY_subjectName, subjectName);
				map.put(KEY_courseDay, courseDay);
				map.put(KEY_courseTime, courseTime);
				map.put(KEY_roomID, roomID);
				map.put(KEY_regCourseType, regCourseType);
				map.put(KEY_lecturerName, lecturerName);
				
				ArrayMap.add(map);					
			} catch (Exception e) {
				e.printStackTrace();
				// TODO: handle exception
				//Log.e(JsonParser_regCourse.class.toString(), "Exception Method GetMapKey");
			}
			
		}		
		return ArrayMap;
	}	
}